package org.springblade.modules.platform.algorithm;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ht.utils.AbsoluteDeflection;
import com.ht.utils.CalModel;
import com.ht.utils.CalModel3;
import com.ht.utils.RelativeDeflection;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.modules.platform.dto.DeviceRuningStateDTO;
import org.springblade.modules.platform.entity.*;
import org.springblade.modules.platform.service.*;
import org.springblade.modules.platform.vo.*;
import org.springblade.modules.platform.vo.enums.*;
import org.springblade.modules.platform.websocket.config.WebSokcetChannelGroup;
import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.util.AttributeKey;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
@AllArgsConstructor
@Slf4j
public class BaseStation4 {
    private final IMeasuringChainPlatformTypeService iMeasuringChainPlatformTypeService;
    private final IMeasuringChainPlatformInfoService iMeasuringChainPlatformInfoService;
    private final IMeasuringPlatformTypeService iMeasuringPlatformTypeService;
    private final IDeviceTableService iDeviceTableService;
    private final IAbsolutelyValueTableService iAbsolutelyValueTableService;
    private final IPhysicalValueTableService iPhysicalValueTableService;//物理量数据
    private final IEngineerValueTableService iEngineerValueTableService;//工程量
    private final IMeasuringPointTableService iMeasuringPointTableService;//一侧量
    private final IEquipmentRuningStateService iEquipmentRuningStateService;
    private final IEquipmentLogManagementService equipmentLogManagementService;
    private final ISysParameterService iSysParameterService;
    private final IProductModelPropertiesService productModelPropertiesService;
    private final IMeasuringChainTableService iMeasuringChainTableService;

    private static final AttributeKey<String> CONNECTION_ID = AttributeKey.valueOf("connectionId");

    /**
     * 2号和17号倾角为0
     * @param deviceRuningStateDTO
     * @param gatherEvent
     */
    public void installDeviceRuningState(DeviceRuningStateDTO deviceRuningStateDTO, Integer gatherEvent) {
        Long acquisitionTime = deviceRuningStateDTO.getAcquisitionTime();//采集时间
        Long deptMeasuringInfoId = deviceRuningStateDTO.getDeptMeasuringInfoId();//测量链编号
        BigDecimal rietschlePressure = deviceRuningStateDTO.getRietschlePressure();//真空泵气压值
        List<DeviceRuningStateDTO.DeviceListDTO> deviceList = deviceRuningStateDTO.getDeviceList();//设备集合
        String acquisitionFlag = deviceRuningStateDTO.getAcquisitionFlag();
        String connectionId = deviceRuningStateDTO.getConnectionId();
        MeasuringChainTable byId = iMeasuringChainTableService.getById(deptMeasuringInfoId);

        List<MeasuringChainPlatformType> measuringChainPlatformTypeList = iMeasuringChainPlatformTypeService.getMeasuringChainPlatformTypeList(deptMeasuringInfoId);
        if(Func.isEmpty(measuringChainPlatformTypeList)){
            return;
        }

        BigDecimal i = new BigDecimal(65).divide(new BigDecimal(22),2,RoundingMode.HALF_UP);
        BigDecimal s = new BigDecimal(30);

        for (MeasuringChainPlatformType x : measuringChainPlatformTypeList) {


            MeasuringChainDetailsVO measuringChainDetailsVO = getMeasuringPlatform(x.getId(), deviceList,x.getMeasuringTypeCode(),acquisitionTime,s,connectionId,acquisitionFlag,i,deptMeasuringInfoId);
            if(measuringChainDetailsVO!=null){
                measuringChainDetailsVO.setGatherTime(acquisitionTime);
                measuringChainDetailsVO.setMeasurChainDirection(x.getMeasuringTypeCode());
                measuringChainDetailsVO.setRietschlePressure(rietschlePressure);
                measuringChainDetailsVO.setGatherEvent(gatherEvent);
                measuringChainDetailsVO.setMeasurChain(byId.getChainName());
                measuringChainDetailsVO.setMeasurChainCode(deptMeasuringInfoId);
                saveMeasuringChainDetails(measuringChainDetailsVO,acquisitionFlag);
            }

            s = measuringChainDetailsVO.getI();
        }
        pullData(new BigDecimal(100),"计算完成",connectionId,200);


    }

    /**
     * 计算测量连，所有侧台的工程量，物理量
     * @param measuringTypeId
     * @param deviceList
     * @return
     */
    private MeasuringChainDetailsVO getMeasuringPlatform(Long measuringTypeId,
                                                         List<DeviceRuningStateDTO.DeviceListDTO> deviceList,
                                                         Integer measuringTypeCode,
                                                         Long acquisitionTime,
                                                         BigDecimal s,
                                                         String connectionId,
                                                         String acquisitionFlag,  BigDecimal i,Long deptMeasuringInfoId ){
        MeasuringChainDetailsVO measuringChainDetailsVO = new MeasuringChainDetailsVO();
        //排序以后所有配置的侧台
        List<MeasuringChainPlatformInfo> measuringChainPlatformInfo = iMeasuringChainPlatformInfoService.getMeasuringChainPlatformInfo(measuringTypeId);
        if(measuringChainPlatformInfo==null){
            return null;
        }
        List<MeasuringPlatformVO> list = new ArrayList<>();
        for (MeasuringChainPlatformInfo chainPlatformInfo : measuringChainPlatformInfo) {
            Long measuringPlatformId = chainPlatformInfo.getMeasuringPlatformId();//侧台编号
            MeasuringPlatformTypeVO measuringPlatformTypeVO = iMeasuringPlatformTypeService.deileMeasuringPlatformType(measuringPlatformId,  measuringTypeCode);//侧台所有计算策略
			Double yAxis = null;
			Long parentPlatformId = chainPlatformInfo.getParentPlatformId();
			if(parentPlatformId!=null){
				MeasuringChainPlatformInfo platformInfo = measuringChainPlatformInfo.stream().filter(x -> x.getMeasuringPlatformId().equals(parentPlatformId)).findFirst().get();
				yAxis = platformInfo.getyAxis();

			}

            MeasuringPlatformVO measuringPlatformVO = measuringPlatformCount(measuringPlatformTypeVO,
                    deviceList,measuringTypeCode,
                    chainPlatformInfo,list,acquisitionTime,s,connectionId,acquisitionFlag,deptMeasuringInfoId,yAxis);
            assert measuringPlatformVO != null;
            measuringPlatformVO.setMeasuringPlatformId(chainPlatformInfo.getMeasuringPlatformId());
            measuringPlatformVO.setMeasuringPlatformName(chainPlatformInfo.getMeasuringPlatformName());
            measuringPlatformVO.setBenchTypeCode(measuringPlatformTypeVO.getMeasuringPlatformType());
            measuringPlatformVO.setBenchTypeName(Objects.requireNonNull(BenchTypeNameEnum.getEnum(measuringPlatformTypeVO.getMeasuringPlatformType())).getMsg());
            measuringPlatformVO.setSort(chainPlatformInfo.getSort());
            list.add(measuringPlatformVO);
            s = s.add(i);

        }
        measuringChainDetailsVO.setMeasuringPlatformVOS(list);
        measuringChainDetailsVO.setMeasurChainCode(measuringTypeId);
        measuringChainDetailsVO.setI(s);
        return measuringChainDetailsVO;
    }







    /**
     * 单个侧台计算物理量，工程量
     * @param measuringPlatformTypeVO
     * @param deviceList
     */
    private MeasuringPlatformVO measuringPlatformCount(MeasuringPlatformTypeVO measuringPlatformTypeVO,
                                                       List<DeviceRuningStateDTO.DeviceListDTO> deviceList,
                                                       Integer measuringTypeCode,
                                                       MeasuringChainPlatformInfo chainPlatformInfo,
                                                       List<MeasuringPlatformVO> measuringPlatformVOS,
                                                       Long acquisitionTime,
                                                       BigDecimal bi,
                                                       String connectionId,
                                                       String acquisitionFlag,
                                                       Long deptMeasuringInfoId,Double yAxis ){
        MeasuringPlatformConfigVO measuringPlatformConfigVO = null;
        List<MeasuringPlatformConfigVO> list1 = measuringPlatformTypeVO.getList();//如果没配置计算策略默认走计算
        if(!Func.isEmpty(list1)){
            measuringPlatformConfigVO = list1.get(0);
        }
        String measuringPlatformName = chainPlatformInfo.getMeasuringPlatformName();
        pullData(bi,"正在计算"+measuringPlatformName+".....",connectionId,200);
        switch (Objects.requireNonNull(BenchTypeNameEnum.getEnum(measuringPlatformTypeVO.getMeasuringPlatformType()))){
            case BASICS://基点测台
                List<OnceValueVO> onceValue1 = new ArrayList<>();
                Map<String,DeviceTableVO> mapd = new HashMap<>();
                List<String> list2 = Arrays.asList(measuringPlatformTypeVO.getDeviceId().split(","));
                list2.forEach(x->{
                    DeviceTableVO deviceTableVO = iDeviceTableService.selectDeviceById(x);
                    mapd.put(deviceTableVO.getDeviceChannelId(),deviceTableVO);
                    String deviceChannelId = deviceTableVO.getDeviceChannelId();
                    Optional<DeviceRuningStateDTO.DeviceListDTO> first = deviceList.stream().filter(l -> l.getDeviceId().equals(deviceChannelId)).findFirst();
                    if(first.isPresent()){
                        DeviceRuningStateDTO.DeviceListDTO deviceListDTO = first.get();//设备上报的本侧台的原始数据
                        List<OnceValueVO> onceValue = getOnceValue(deviceListDTO, acquisitionTime,x,acquisitionFlag);
                        onceValue1.addAll(onceValue);
                    }

                });
                List<String> list = Arrays.asList(measuringPlatformTypeVO.getDeviceChannelId().split(","));

                AbsoluteValueVO absoluteValue = getAbsoluteValue(list,mapd, measuringPlatformConfigVO, deviceList,measuringPlatformTypeVO.getId(),acquisitionTime);//工程量
                List<String> stringList = absoluteValue.getStringList();
                if(!Func.isEmpty(stringList)){
                    List<String> collect1 = stringList.stream().distinct().collect(Collectors.toList());
                    collect1.forEach(c->{
                        DeviceTableVO deviceTableVO = iDeviceTableService.selectDeviceById(c);
                        String deviceChannelId = deviceTableVO.getDeviceChannelId();
                        Optional<DeviceRuningStateDTO.DeviceListDTO> first = deviceList.stream().filter(l -> l.getDeviceId().equals(deviceChannelId)).findFirst();
                        if(first.isPresent()){
                            DeviceRuningStateDTO.DeviceListDTO deviceListDTO = first.get();//设备上报的本侧台的原始数据
                            List<OnceValueVO> onceValue = getOnceValue(deviceListDTO, acquisitionTime,c,acquisitionFlag);
                            onceValue1.addAll(onceValue);
                        }
                    });
                }
                /* onceValue1.addAll(onceValue5);*/
                return  MeasuringPlatformVO.builder().physicalQuantityVO(null).absoluteValueVO(absoluteValue).onceValueVOList(onceValue1).build();
            case ORDINARY://普通测台
                String deviceChannelId = measuringPlatformTypeVO.getDeviceChannelId();
                String deviceId = measuringPlatformTypeVO.getDeviceId();
                DeviceRuningStateDTO.DeviceListDTO deviceListDTO = null;
                Optional<DeviceRuningStateDTO.DeviceListDTO> first = deviceList.stream().filter(l -> l.getDeviceId().equals(deviceChannelId)).findFirst();
                if(first.isPresent()){
                    deviceListDTO = first.get();//设备上报的本侧台的原始数据
                }else {
                    //查询默认值
                }
                List<OnceValueVO> onceValue = getOnceValue(deviceListDTO, acquisitionTime,deviceId,acquisitionFlag);
                Long parentPlatformId = chainPlatformInfo.getParentPlatformId();//付侧台

                log.info("计算物理量开始:{}",measuringPlatformTypeVO.getMeasuringPlatformName());
                AbsoluteValueVO ordinaryAbsoluteValue;
                PhysicalQuantityVO physicalQuantity = getPhysicalQuantity(deviceListDTO, parentPlatformId, measuringTypeCode,deviceList,measuringPlatformTypeVO.getId(), deviceId, deptMeasuringInfoId);//物理量
                if(parentPlatformId!=null){
                    MeasuringPlatformVO parentPlatformVO = measuringPlatformVOS.stream().filter(l -> l.getMeasuringPlatformId().equals(chainPlatformInfo.getParentPlatformId())).findFirst().get();//付侧台工程量和物理量

                    if(measuringPlatformConfigVO==null){
                        ordinaryAbsoluteValue = getOrdinaryAbsoluteValue(measuringPlatformTypeVO.getId(),measuringPlatformConfigVO, null, parentPlatformVO, chainPlatformInfo,physicalQuantity,measuringTypeCode, yAxis );//工程量
                    }else {
                        ordinaryAbsoluteValue = getOrdinaryAbsoluteValue(measuringPlatformTypeVO.getId(),measuringPlatformConfigVO, deviceList, parentPlatformVO, chainPlatformInfo, physicalQuantity,measuringTypeCode, yAxis );//工程量
                    }
                }else {
                    //需要知道高精度传感器的值
                    ordinaryAbsoluteValue = getOrdinaryAbsoluteValue2(measuringPlatformConfigVO, deviceList,measuringTypeCode);//高精度传感器带入
                    //   ordinaryAbsoluteValue = AbsoluteValueVO.builder().x(0.0).y(0.0).z(0.0).alpha(0.0).beta(0.0).gama(0.0).build();
                    physicalQuantity.setDipX(ordinaryAbsoluteValue.getAlpha());
                    physicalQuantity.setDipY(ordinaryAbsoluteValue.getBeta());
                }
                List<String> stringList1 = ordinaryAbsoluteValue.getStringList();
                if(!Func.isEmpty(stringList1)){
                    List<String> collect1 = stringList1.stream().distinct().collect(Collectors.toList());
                    collect1.forEach(c->{
                        DeviceTableVO deviceTableVO = iDeviceTableService.selectDeviceById(c);
                        Optional<DeviceRuningStateDTO.DeviceListDTO> first1 = deviceList.stream().filter(l -> l.getDeviceId().equals(deviceTableVO.getDeviceChannelId())).findFirst();
                        if(first1.isPresent()){
                            DeviceRuningStateDTO.DeviceListDTO deviceListDTO1 = first1.get();//设备上报的本侧台的原始数据
                            List<OnceValueVO> onceValue2 = getOnceValue(deviceListDTO1, acquisitionTime,c,acquisitionFlag);
                            onceValue.addAll(onceValue2);
                        }
                    });
                }

                log.info("侧台名称:{}",measuringPlatformTypeVO.getMeasuringPlatformName());
                log.info("物理量:{}",physicalQuantity);
                log.info("工程量:{}",ordinaryAbsoluteValue);
                /*   onceValue.addAll(onceValue5);*/
                return  MeasuringPlatformVO.builder().physicalQuantityVO(physicalQuantity).absoluteValueVO(ordinaryAbsoluteValue).onceValueVOList(onceValue).build();
        }

        return null;
    }
    /**
     *普通侧台工程量
     * @param measuringPlatformConfigVO(测量连方向)
     * @param deviceList（父侧台工程量）
     * @return
     */
    private AbsoluteValueVO getOrdinaryAbsoluteValue2(MeasuringPlatformConfigVO measuringPlatformConfigVO,
                                                      List<DeviceRuningStateDTO.DeviceListDTO> deviceList,Integer measuringTypeCode){//1正向 2逆向
        Double alpha = 0.0;
        Double beta = 0.0;
        Double gama = 0.0;
        Double x = 0.0;
        Double y = 0.0;
        Double z = 0.0;

        List<String> strings = new ArrayList<>();
        if(measuringPlatformConfigVO!=null){//走配置的策略

            Integer xType = measuringPlatformConfigVO.getxType();
            if(xType!=null){
                if(xType.equals(DataStrategyEnum.MANUAL.getCode())){
                    x = Double.parseDouble(measuringPlatformConfigVO.getxValue());
                }else {
                    //未给出结算规则
                }
            }
            Integer yType = measuringPlatformConfigVO.getyType();
            if(yType!=null){
                if(yType.equals(DataStrategyEnum.MANUAL.getCode())){
                    y = Double.parseDouble(measuringPlatformConfigVO.getyValue());
                }else {
                    //未给出结算规则
                }
            }
            Integer zType = measuringPlatformConfigVO.getzType();
            if(zType!=null){
                if(zType.equals(DataStrategyEnum.MANUAL.getCode())){
                    z = Double.parseDouble(measuringPlatformConfigVO.getzValue());
                }else {
                    //未给出结算规则
                }
            }
            Map<String, Double> dataStrategy = null;
            Integer alphaType = measuringPlatformConfigVO.getAlphaType();
            if(alphaType!=null){
                if(alphaType.equals(DataStrategyEnum.MANUAL.getCode())){
                    alpha = Double.parseDouble(measuringPlatformConfigVO.getAlphaValue());
                }else if(alphaType.equals(DataStrategyEnum.COMPANY.getCode())){
                    dataStrategy =  getDataStrategy(measuringPlatformConfigVO.getMeasuringPlatformId(),measuringPlatformConfigVO.getAlphaValue(),measuringPlatformConfigVO.getPlatformTypeCode(),deviceList);//ccd1AfterDipx1
                    alpha = dataStrategy.get("dipX");

                    strings.add(measuringPlatformConfigVO.getAlphaValue());
                }
            }
            Integer betaType = measuringPlatformConfigVO.getBetaType();
            if(betaType!=null){
                if(betaType.equals(DataStrategyEnum.MANUAL.getCode())){
                    beta = Double.parseDouble(measuringPlatformConfigVO.getBetaValue());
                }else if(betaType.equals(DataStrategyEnum.COMPANY.getCode())){
                    beta = dataStrategy.get("dipY");
                    strings.add(measuringPlatformConfigVO.getBetaValue());
                }
            }
            Integer gamaType = measuringPlatformConfigVO.getGamaType();
            if(gamaType!=null){
                if(gamaType.equals(DataStrategyEnum.MANUAL.getCode())){
                    gama = Double.parseDouble(measuringPlatformConfigVO.getGamaValue());
                }else if(gamaType.equals(DataStrategyEnum.COMPANY.getCode())){
                    //没有提供算法
                }
            }
        }
        //gama = 4.85E-6 * -3;
   /*    if(measuringTypeCode==2){
            alpha = alpha * -1 ;
        }*/
        log.info("程量为:{}",AbsoluteValueVO.builder().x(x).y(y).z(z).alpha(alpha).beta(beta).gama(gama).stringList(strings).build());
        return AbsoluteValueVO.builder().x(x).y(y).z(z).alpha(alpha).beta(beta).gama(gama).stringList(strings).build();

    }
    /**
     *普通侧台工程量
     * @param measuringPlatformConfigVO(测量连方向)
     * @param deviceList（父侧台工程量）
     * @param parentPlatformVO（本侧台的工程量）
     * @param chainPlatformInfo（本侧台的物理量）
     * @return
     */
    private AbsoluteValueVO getOrdinaryAbsoluteValue(Long measuringPlatformId,MeasuringPlatformConfigVO measuringPlatformConfigVO,
                                                     List<DeviceRuningStateDTO.DeviceListDTO> deviceList,
                                                     MeasuringPlatformVO parentPlatformVO,
                                                     MeasuringChainPlatformInfo chainPlatformInfo,
                                                     PhysicalQuantityVO physicalQuantity,
                                                     Integer measuringTypeCode,Double yAxis ){
        AbsoluteValueVO absoluteValueVO = parentPlatformVO.getAbsoluteValueVO();
        Double x;
        Double y;
        Double z;
        Double alpha;
        Double beta;
        Double gama;
        PhysicalQuantityVO physicalQuantityVO = parentPlatformVO.getPhysicalQuantityVO();//付侧台的物理量
        Boolean dir = Objects.equals(measuringTypeCode,1);//测量连方向

        AbsoluteDeflection parentDeflect = new AbsoluteDeflection();//父侧台工程量
        parentDeflect.setZ(absoluteValueVO.getZ());
        parentDeflect.setX(absoluteValueVO.getX());
        parentDeflect.setY(absoluteValueVO.getY());
        parentDeflect.setAlpha(absoluteValueVO.getAlpha());
        parentDeflect.setBeta(absoluteValueVO.getBeta());
        parentDeflect.setGama(absoluteValueVO.getGama());


        AbsoluteDeflection childDeflect = new AbsoluteDeflection();//本侧台的工程量
        childDeflect.setBeta(physicalQuantity.getDipY());


        RelativeDeflection relativeDeflect = new RelativeDeflection();//本侧台的物理量
        relativeDeflect.setNX(physicalQuantity.getNX());
        relativeDeflect.setNZ(physicalQuantity.getNZ());
        relativeDeflect.setZX(physicalQuantity.getZX());
        relativeDeflect.setZZ(physicalQuantity.getZZ());


        if(!dir){//如果是逆向的时候DeltaY取父级测台的DeltaY
            Double deltaY = physicalQuantityVO.getDeltaY();
            relativeDeflect.setDeltaY(deltaY);
        }else {
            relativeDeflect.setDeltaY(physicalQuantity.getDeltaY());
        }
        CalModel calModel = new CalModel();
        AbsoluteDeflection cal = calModel.cal(dir,
                parentDeflect,
                childDeflect,
                relativeDeflect,
                chainPlatformInfo.getyAxis(),
                chainPlatformInfo.getPreviousDistance(),
                chainPlatformInfo.getxAxis(),yAxis);

		x = cal.getX();
		alpha = cal.getAlpha();
		//alpha  = physicalQuantity.getDipX();
		y = cal.getY();
		z = cal.getZ();
		//beta = physicalQuantity.getDipY();
		beta = cal.getBeta();//倾角Y



		gama = cal.getGama();
        List<String> strings = new ArrayList<>();
        if(measuringPlatformConfigVO!=null){//走配置的策略

            Integer xType = measuringPlatformConfigVO.getxType();
            if(xType!=null){
                if(xType.equals(DataStrategyEnum.MANUAL.getCode())){
                    x = Double.parseDouble(measuringPlatformConfigVO.getxValue());
                }else {
                    //未给出结算规则
                }
            }
            Integer yType = measuringPlatformConfigVO.getyType();
            if(yType!=null){
                if(yType.equals(DataStrategyEnum.MANUAL.getCode())){
                    y = Double.parseDouble(measuringPlatformConfigVO.getyValue());
                }else {
                    //未给出结算规则
                }
            }
            Integer zType = measuringPlatformConfigVO.getzType();
            if(zType!=null){
                if(zType.equals(DataStrategyEnum.MANUAL.getCode())){
                    z = Double.parseDouble(measuringPlatformConfigVO.getzValue());
                }else {
                    //未给出结算规则
                }
            }
            Map<String, Double> dataStrategy;
            Integer alphaType = measuringPlatformConfigVO.getAlphaType();
            if(alphaType!=null){
                if(alphaType.equals(DataStrategyEnum.MANUAL.getCode())){
                    alpha = Double.parseDouble(measuringPlatformConfigVO.getAlphaValue());
                }else if(alphaType.equals(DataStrategyEnum.COMPANY.getCode())){
                    dataStrategy =  getDataStrategy(measuringPlatformConfigVO.getMeasuringPlatformId(),measuringPlatformConfigVO.getAlphaValue(),measuringPlatformConfigVO.getPlatformTypeCode(),deviceList);//ccd1AfterDipx1
                    alpha = dataStrategy.get("dipX");

                    strings.add(measuringPlatformConfigVO.getAlphaValue());
                }
            }
            Integer betaType = measuringPlatformConfigVO.getBetaType();
            if(betaType!=null){
                if(betaType.equals(DataStrategyEnum.MANUAL.getCode())){
                    beta = Double.parseDouble(measuringPlatformConfigVO.getBetaValue());
                }else if(betaType.equals(DataStrategyEnum.COMPANY.getCode())){
                    dataStrategy = getDataStrategy(measuringPlatformConfigVO.getMeasuringPlatformId(),measuringPlatformConfigVO.getBetaValue(), measuringPlatformConfigVO.getPlatformTypeCode(), deviceList);
                    beta = dataStrategy.get("dipY");
                    strings.add(measuringPlatformConfigVO.getBetaValue());
                }
            }
            Integer gamaType = measuringPlatformConfigVO.getGamaType();
            if(gamaType!=null){
                if(gamaType.equals(DataStrategyEnum.MANUAL.getCode())){
                    gama = Double.parseDouble(measuringPlatformConfigVO.getGamaValue());
                }else if(gamaType.equals(DataStrategyEnum.COMPANY.getCode())){
                    //没有提供算法
                }
            }
        }
        return AbsoluteValueVO.builder().x(x).y(y).z(z).alpha(alpha).beta(beta).gama(gama).stringList(strings).build();
    }
    /**
     * 普通测台计算物理量
     * @param deviceListDTO
     * @param parentPlatformId
     * @return
     */
    private PhysicalQuantityVO getPhysicalQuantity(DeviceRuningStateDTO.DeviceListDTO deviceListDTO,
                                                   Long parentPlatformId,
                                                   Integer measuringTypeCode,
                                                   List<DeviceRuningStateDTO.DeviceListDTO> deviceList,Long id,String deviceId,Long deptMeasuringInfoId){


        List<SysParameter> sysParameterList = iSysParameterService.list(Wrappers.<SysParameter>query().lambda().eq(SysParameter::getMeasuringPlatformId,id));
        List<DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO> attributeListDTO = deviceListDTO.getAttributeListDTO();
        MeasuringPlatformTypeVO parentPlatformTypeVO = iMeasuringPlatformTypeService.deileMeasuringPlatformType(parentPlatformId,measuringTypeCode);
        if(parentPlatformTypeVO!=null&&Objects.equals(parentPlatformTypeVO.getMeasuringPlatformType(), BenchTypeNameEnum.BASICS.getCode())){//付侧台位基点侧台

            AbsolutelyValueTable one = iAbsolutelyValueTableService.getOne(Wrappers.<AbsolutelyValueTable>lambdaQuery()
                    .eq(AbsolutelyValueTable::getDeptId,deviceId)
                    .eq(AbsolutelyValueTable::getMeasurChainDirection,measuringTypeCode));
            Integer ccd3_after_tangential_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_after_tangential_y")).findFirst().get().getValue();
            Integer ccd3_front_tangential_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_front_tangential_y")).findFirst().get().getValue();
            // Double deltaY = baseStationAlgorithm.getDeformation(ccd3_after_tangential_y,ccd3_front_tangential_y);
            Double deltaY = getModelData(Arrays.asList(ccd3_after_tangential_y, ccd3_front_tangential_y), sysParameterList,PhysicalQuantityEnum.DELTAY);

            Integer ccd1_after_dip_x = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_after_dip_y")).findFirst().get().getValue();
            Integer ccd1_front_dip_x = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_front_dip_y")).findFirst().get().getValue();
            //Double dipX = (ccd1_after_dip_x + ccd1_front_dip_x) / 2 * 0.0047 / 2000;
            Double dipX = getModelData(Arrays.asList(ccd1_after_dip_x, ccd1_front_dip_x), sysParameterList,PhysicalQuantityEnum.DIPX);


            Integer ccd2_after_dip_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_after_dip_x")).findFirst().get().getValue();
            Integer ccd2_front_dip_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_front_dip_x")).findFirst().get().getValue();
            //  Double dipY = (ccd2_after_dip_y+ ccd2_front_dip_y ) / 2 * 0.0047 / 2000;
            Double dipY = getModelData(Arrays.asList(ccd2_after_dip_y, ccd2_front_dip_y), sysParameterList,PhysicalQuantityEnum.DIPY);
            if(one==null){
                one = new AbsolutelyValueTable();
                one.setMeasurChainCode(deptMeasuringInfoId);
                one.setMeasurChainDirection(measuringTypeCode);
                one.setDeptId(deviceId);
                one.setzX(0.0);
                one.setnX(0.0);
                one.setzZ(0.0);
                one.setnZ(0.0);


                one.setDipY(dipY);
                one.setDipX(dipX);
                one.setDeltaY(deltaY);
                one.setInsertTime(System.currentTimeMillis());
                iAbsolutelyValueTableService.save(one);
            }
            dipY = dipY - one.getDipY();
            dipX = dipX - one.getDipX();
            deltaY = deltaY - one.getDeltaY();
            return PhysicalQuantityVO.builder().zX(0.0).nZ(0.0).zZ(0.0).zZ(0.0).nX(0.0).deltaY(deltaY).dipX(dipX).dipY(dipY).build();
        }else {
            AbsolutelyValueTable one = iAbsolutelyValueTableService.getOne(Wrappers.<AbsolutelyValueTable>lambdaQuery()
                    .eq(AbsolutelyValueTable::getDeptId,deviceId)
                    .eq(AbsolutelyValueTable::getMeasurChainDirection,measuringTypeCode));


            if(measuringTypeCode==1){//正向
                if(parentPlatformId==null){
                    Double dipY = 0.0;
                    Double deltaY = 0.0;
                    Double dipX = 0.0;
                    if(one==null){
                        one = new AbsolutelyValueTable();
                        one.setMeasurChainCode(deptMeasuringInfoId);
                        one.setMeasurChainDirection(measuringTypeCode);
                        one.setDeptId(deviceId);
                        one.setzX(0.0);
                        one.setnX(0.0);
                        one.setzZ(0.0);
                        one.setnZ(0.0);
                        one.setDipY(0.0);
                        one.setDipX(0.0);
                        one.setDeltaY(0.0);
                        one.setInsertTime(System.currentTimeMillis());
                        iAbsolutelyValueTableService.save(one);
                    }
                    dipY = dipY - one.getDipY();
                    dipX = dipX - one.getDipX();
                    deltaY = deltaY - one.getDeltaY();
                    return PhysicalQuantityVO.builder().zX(0.0).nZ(0.0).zZ(0.0).zZ(0.0).nX(0.0).deltaY(deltaY).dipX(dipX).dipY(dipY).build();
                }


                Integer ccd1_x_front_forward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_x_front_forward")).findFirst().get().getValue();
                Integer ccd1_x_after_forward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_x_after_forward")).findFirst().get().getValue();
                Integer ccd4_x_after_forward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd4_x_after_forward")).findFirst().get().getValue();
                Integer ccd4_x_front_forward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd4_x_front_forward")).findFirst().get().getValue();
                // Double zx = baseStationAlgorithm.getPhysicalQuantityFormula(ccd1_x_after_forward, ccd1_x_front_forward, ccd4_x_after_forward, ccd4_x_front_forward);
                log.info("zxccd值:{}",Arrays.asList(ccd1_x_after_forward, ccd1_x_front_forward,ccd4_x_after_forward,ccd4_x_front_forward));
                Double zx = getModelData(Arrays.asList(ccd1_x_after_forward, ccd1_x_front_forward,ccd4_x_after_forward,ccd4_x_front_forward), sysParameterList,PhysicalQuantityEnum.ZX);
                log.info("zxccd值:{}",Arrays.asList(ccd1_x_after_forward,ccd4_x_after_forward));
                Double zx2 = getModelData(Arrays.asList(ccd1_x_after_forward,ccd4_x_after_forward), sysParameterList,PhysicalQuantityEnum.ZX);
                log.info("zxccd值:{}",Arrays.asList(ccd1_x_front_forward,ccd4_x_front_forward));
                Double zx3 = getModelData(Arrays.asList( ccd1_x_front_forward,ccd4_x_front_forward), sysParameterList,PhysicalQuantityEnum.ZX);

                //单独计算前沿  和单独计算后延

                Integer ccd2_z_after_forward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_z_after_forward")).findFirst().get().getValue();
                Integer ccd2_z_front_forward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_z_front_forward")).findFirst().get().getValue();
                Integer ccd3_z_after_forward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_z_after_forward")).findFirst().get().getValue();
                Integer ccd3_z_front_forward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_z_front_forward")).findFirst().get().getValue();
                // Double zz = baseStationAlgorithm.getPhysicalQuantityFormula(ccd2_z_after_forward, ccd2_z_front_forward, ccd3_z_after_forward, ccd3_z_front_forward);
                Double zz = getModelData(Arrays.asList(ccd2_z_after_forward, ccd2_z_front_forward,ccd3_z_after_forward,ccd3_z_front_forward), sysParameterList,PhysicalQuantityEnum.ZZ);
                Double zz2 = getModelData(Arrays.asList(ccd2_z_after_forward,ccd3_z_after_forward), sysParameterList,PhysicalQuantityEnum.ZZ);
                Double zz3 = getModelData(Arrays.asList(ccd2_z_front_forward,ccd3_z_front_forward), sysParameterList,PhysicalQuantityEnum.ZZ);



                String deviceChannelId = parentPlatformTypeVO.getDeviceChannelId();
                List<DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO> parentDTO = null;
                Optional<DeviceRuningStateDTO.DeviceListDTO> first = deviceList.stream().filter(l -> l.getDeviceId().equals(deviceChannelId)).findFirst();
                if(first.isPresent()){
                    parentDTO = first.get().getAttributeListDTO();
                }else {
                    //读取默认值
                }
                assert parentDTO != null;

                //List<MeasuringPlatformConfigVO> list = parentPlatformTypeVO.getList();
                //高精度传感器带入计算的


                Integer ccd1_x_after_backward = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_x_after_backward")).findFirst().get().getValue();
                Integer ccd1_x_front_backward = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_x_front_backward")).findFirst().get().getValue();
                Integer ccd4_x_after_backward = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd4_x_after_backward")).findFirst().get().getValue();
                Integer ccd4_x_front_backward = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd4_x_front_backward")).findFirst().get().getValue();

                List<SysParameter> parentSysParameterList = iSysParameterService.list(Wrappers.<SysParameter>query().lambda().eq(SysParameter::getMeasuringPlatformId, parentPlatformTypeVO.getId()));

                Double nx = getModelData(Arrays.asList(ccd1_x_after_backward, ccd1_x_front_backward,ccd4_x_after_backward,ccd4_x_front_backward), parentSysParameterList,PhysicalQuantityEnum.NX);
                Double nx2 = getModelData(Arrays.asList(ccd1_x_after_backward,ccd4_x_after_backward), parentSysParameterList,PhysicalQuantityEnum.NX);
                Double nx3 = getModelData(Arrays.asList( ccd1_x_front_backward,ccd4_x_front_backward), parentSysParameterList,PhysicalQuantityEnum.NX);

                //CCD2-Z脉冲后沿-后向位移
                Integer ccd2_z_after_backward = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_z_after_backward")).findFirst().get().getValue();
                //CCD2-Z脉冲前沿-后向位移
                Integer ccd2_z_front_backward = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_z_front_backward")).findFirst().get().getValue();
                //CCD3-Z脉冲后沿-后向位移
                Integer ccd3_z_after_backward = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_z_after_backward")).findFirst().get().getValue();
                //CCD3-Z脉冲前沿-后向位移
                Integer ccd3_z_front_backward = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_z_front_backward")).findFirst().get().getValue();

                Double nz = getModelData(Arrays.asList(ccd2_z_after_backward, ccd2_z_front_backward,ccd3_z_after_backward,ccd3_z_front_backward), parentSysParameterList,PhysicalQuantityEnum.NZ);
                Double nz2 = getModelData(Arrays.asList(ccd2_z_after_backward,ccd3_z_after_backward), parentSysParameterList,PhysicalQuantityEnum.NZ);
                Double nz3 = getModelData(Arrays.asList(ccd2_z_front_backward,ccd3_z_front_backward), parentSysParameterList,PhysicalQuantityEnum.NZ);


                Integer ccd3_after_tangential_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_after_tangential_y")).findFirst().get().getValue();
                Integer ccd3_front_tangential_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_front_tangential_y")).findFirst().get().getValue();

                Integer ccd2_after_dip_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_after_dip_y")).findFirst().get().getValue();
                Integer ccd2_front_dip_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_front_dip_y")).findFirst().get().getValue();

                Integer ccd1_after_dip_x = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_after_dip_x")).findFirst().get().getValue();
                Integer ccd1_front_dip_x = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_front_dip_x")).findFirst().get().getValue();

                if(one==null){
                    one = new AbsolutelyValueTable();
                    one.setMeasurChainCode(deptMeasuringInfoId);
                    one.setMeasurChainDirection(measuringTypeCode);
                    one.setDeptId(deviceId);
                    one.setzX(zx);
                    one.setzZ(zz);
                    one.setnX(nx);
                    one.setnZ(nz);

                    one.setCcd2FrontDipY(ccd2_front_dip_y);
                    one.setCcd2AfterDipY(ccd2_after_dip_y);

                    one.setCcd1AfterDipX(ccd1_after_dip_x);
                    one.setCcd1FrontDipX(ccd1_front_dip_x);

                    one.setCcd3AfterTangentialY(ccd3_after_tangential_y);
                    one.setCcd3FrontTangentialY(ccd3_front_tangential_y);

                    one.setInsertTime(System.currentTimeMillis());
                    iAbsolutelyValueTableService.save(one);
                    return PhysicalQuantityVO.builder().nZ(0.0).zX(0.0).zZ(0.0).nX(0.0).deltaY(0.0).dipY(0.0).dipX(0.0).build();
                }


                zx =  getDobleMath(zx - one.getzX(),zx2 - one.getzX(),zx3 - one.getzX());
                zz = getDobleMath(zz - one.getzZ(), zz2 - one.getzZ(), zz3 - one.getzZ());
                nx = getDobleMath(nx - one.getnX(), nx2 - one.getnX(), nx3 - one.getnX());
                nz = getDobleMath(nz - one.getnZ(), nz2 - one.getnZ(), nz3 - one.getnZ());

                List<Double> dobleMath1 = getDobleMath2(ccd3_front_tangential_y , ccd3_after_tangential_y ,one.getCcd3FrontTangentialY(),one.getCcd3AfterTangentialY());
                Double deltaY = getModelData2(dobleMath1.get(0), sysParameterList, PhysicalQuantityEnum.DELTAY)
                              - getModelData2(dobleMath1.get(1), sysParameterList, PhysicalQuantityEnum.DELTAY);


                List<Double> dobleMath2 = getDobleMath2(ccd2_after_dip_y , ccd2_front_dip_y,one.getCcd2AfterDipY(),one.getCcd2FrontDipY());
                Double dipX = getModelData2(dobleMath2.get(0), sysParameterList, PhysicalQuantityEnum.DIPY)
                            - getModelData2(dobleMath2.get(1), sysParameterList, PhysicalQuantityEnum.DIPY);



                List<Double> dobleMath3 = getDobleMath2(ccd1_front_dip_x , ccd1_after_dip_x,one.getCcd1FrontDipX() ,one.getCcd1AfterDipX());
                Double dipY = getModelData2(dobleMath3.get(0), sysParameterList,PhysicalQuantityEnum.DIPX)
                            - getModelData2(dobleMath3.get(1), sysParameterList, PhysicalQuantityEnum.DIPX);

                return PhysicalQuantityVO.builder().nZ(nz).zX(zx).zZ(zz).nX(nx).deltaY(deltaY).dipY(dipY).dipX(dipX).build();
            }else {//逆向
                if(parentPlatformId==null){
                    Integer ccd3_after_tangential_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_after_tangential_y")).findFirst().get().getValue();
                    Integer ccd3_front_tangential_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_front_tangential_y")).findFirst().get().getValue();
                    log.info("1逆向deltaY值:{}",Arrays.asList(ccd3_after_tangential_y, ccd3_front_tangential_y));
                    Double dipX = 0.0;
                    Double dipY = 0.0;
                    if(one==null){
                        one = new AbsolutelyValueTable();
                        one.setMeasurChainDirection(measuringTypeCode);
                        one.setDeptId(deviceId);
                        one.setzX(0.0);
                        one.setnX(0.0);
                        one.setzZ(0.0);
                        one.setnZ(0.0);
                        one.setDipY(0.0);
                        one.setMeasurChainCode(deptMeasuringInfoId);
                        one.setDipX(0.0);

                        one.setCcd3AfterTangentialY(ccd3_after_tangential_y);
                        one.setCcd3FrontTangentialY(ccd3_front_tangential_y);

                        one.setInsertTime(System.currentTimeMillis());
                        iAbsolutelyValueTableService.save(one);
                    }
                    dipY = dipY - one.getDipY();
                    dipX = dipX - one.getDipX();

                    List<Double> dobleMath1 = getDobleMath2(ccd3_front_tangential_y , ccd3_after_tangential_y ,one.getCcd3FrontTangentialY(),one.getCcd3AfterTangentialY());
                    Double deltaY = getModelData2(dobleMath1.get(0), sysParameterList, PhysicalQuantityEnum.DELTAY)
                            - getModelData2(dobleMath1.get(1), sysParameterList, PhysicalQuantityEnum.DELTAY);

                    return PhysicalQuantityVO.builder().zX(0.0).nZ(0.0).zZ(0.0).zZ(0.0).nX(0.0).deltaY(deltaY).dipX(dipX).dipY(dipY).build();
                }
                Integer ccd1_x_front_backward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_x_front_backward")).findFirst().get().getValue();
                Integer ccd1_x_after_backward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_x_after_backward")).findFirst().get().getValue();
                Integer ccd4_x_after_backward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd4_x_after_backward")).findFirst().get().getValue();
                Integer ccd4_x_front_backward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd4_x_front_backward")).findFirst().get().getValue();
                //Double zx = baseStationAlgorithm.getPhysicalQuantityFormula(ccd1_x_front_backward, ccd1_x_after_backward, ccd4_x_after_backward, ccd4_x_front_backward);
                log.info("1逆向ccd值:{}",Arrays.asList(ccd1_x_front_backward, ccd1_x_after_backward,ccd4_x_after_backward,ccd4_x_front_backward));
                Double zx = getModelData(Arrays.asList(ccd1_x_front_backward, ccd1_x_after_backward,ccd4_x_after_backward,ccd4_x_front_backward), sysParameterList,PhysicalQuantityEnum.ZX);
                log.info("2逆向ccd值:{}",Arrays.asList(ccd1_x_after_backward,ccd4_x_after_backward));
                Double zx2 = getModelData(Arrays.asList(ccd1_x_after_backward,ccd4_x_after_backward), sysParameterList,PhysicalQuantityEnum.ZX);

                log.info("3逆向ccd值:{}",Arrays.asList(ccd1_x_front_backward,ccd4_x_front_backward));
                Double zx3 = getModelData(Arrays.asList(ccd1_x_front_backward,ccd4_x_front_backward), sysParameterList,PhysicalQuantityEnum.ZX);

                Integer ccd2_z_after_backward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_z_after_backward")).findFirst().get().getValue();
                Integer ccd2_z_front_backward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_z_front_backward")).findFirst().get().getValue();
                Integer ccd3_z_after_backward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_z_after_backward")).findFirst().get().getValue();
                Integer ccd3_z_front_backward = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_z_front_backward")).findFirst().get().getValue();
                // Double zz = baseStationAlgorithm.getPhysicalQuantityFormula(ccd2_z_after_backward, ccd2_z_front_backward, ccd3_z_after_backward, ccd3_z_front_backward);
                Double zz = getModelData(Arrays.asList(ccd2_z_after_backward, ccd2_z_front_backward,ccd3_z_after_backward,ccd3_z_front_backward), sysParameterList,PhysicalQuantityEnum.ZZ);;
                Double zz2 = getModelData(Arrays.asList(ccd2_z_after_backward,ccd3_z_after_backward), sysParameterList,PhysicalQuantityEnum.ZZ);
                Double zz3 = getModelData(Arrays.asList(ccd2_z_front_backward,ccd3_z_front_backward), sysParameterList,PhysicalQuantityEnum.ZZ);


                String deviceChannelId = parentPlatformTypeVO.getDeviceChannelId();
                List<DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO> parentDTO = null;
                Optional<DeviceRuningStateDTO.DeviceListDTO> first = deviceList.stream().filter(l -> l.getDeviceId().equals(deviceChannelId)).findFirst();
                if(first.isPresent()){
                    parentDTO = first.get().getAttributeListDTO();
                }else {
                    //读取默认值
                }
                assert parentDTO != null;
                Integer ccd1_x_after_forward_p = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_x_after_forward")).findFirst().get().getValue();
                Integer ccd1_x_front_forward_p = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_x_front_forward")).findFirst().get().getValue();
                Integer ccd4_x_after_forward_p = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd4_x_after_forward")).findFirst().get().getValue();
                Integer ccd4_x_front_forward_p = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd4_x_front_forward")).findFirst().get().getValue();
                //Double nx = baseStationAlgorithm.getPhysicalQuantityFormula(ccd1_x_after_forward_p, ccd1_x_front_forward_p, ccd4_x_after_forward_p, ccd4_x_front_forward_p);
                List<SysParameter> parentSysParameterList = iSysParameterService.list(Wrappers.<SysParameter>query().lambda().eq(SysParameter::getMeasuringPlatformId, parentPlatformTypeVO.getId()));
                Double nx = getModelData(Arrays.asList(ccd1_x_after_forward_p,ccd1_x_front_forward_p,ccd4_x_after_forward_p,ccd4_x_front_forward_p), parentSysParameterList,PhysicalQuantityEnum.NX);;
                Double nx2 = getModelData(Arrays.asList(ccd1_x_after_forward_p,ccd4_x_after_forward_p), parentSysParameterList,PhysicalQuantityEnum.NX);;
                Double nx3 = getModelData(Arrays.asList(ccd1_x_front_forward_p,ccd4_x_front_forward_p), parentSysParameterList,PhysicalQuantityEnum.NX);;


                Integer ccd2_z_after_forward_p = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_z_after_forward")).findFirst().get().getValue();
                Integer ccd2_z_front_forward_p = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_z_front_forward")).findFirst().get().getValue();
                Integer ccd3_z_after_forward_p = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_z_after_forward")).findFirst().get().getValue();
                Integer ccd3_z_front_forward_p = parentDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_z_front_forward")).findFirst().get().getValue();
                //Double nz = baseStationAlgorithm.getPhysicalQuantityFormula(ccd2_z_after_forward_p, ccd2_z_front_forward_p, ccd3_z_after_forward_p, ccd3_z_front_forward_p);
                Double nz = getModelData(Arrays.asList(ccd2_z_after_forward_p,ccd2_z_front_forward_p,ccd3_z_after_forward_p,ccd3_z_front_forward_p), parentSysParameterList,PhysicalQuantityEnum.NZ);;
                Double nz2 = getModelData(Arrays.asList(ccd2_z_after_forward_p,ccd3_z_after_forward_p), parentSysParameterList,PhysicalQuantityEnum.NZ);
                Double nz3 = getModelData(Arrays.asList(ccd2_z_front_forward_p,ccd3_z_front_forward_p), parentSysParameterList,PhysicalQuantityEnum.NZ);


                Integer ccd3_after_tangential_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_after_tangential_y")).findFirst().get().getValue();
                Integer ccd3_front_tangential_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd3_front_tangential_y")).findFirst().get().getValue();
                // Double deltaY = baseStationAlgorithm.getDeformation(ccd3_after_tangential_y,ccd3_front_tangential_y);

             /*   log.info("1逆向deltaY值:{}",Arrays.asList(ccd3_after_tangential_y, ccd3_front_tangential_y));
                Double deltaY = getModelData(Arrays.asList(ccd3_after_tangential_y,ccd3_front_tangential_y), sysParameterList,PhysicalQuantityEnum.DELTAY);
                log.info("2逆向deltaY值:{}",Arrays.asList(ccd3_after_tangential_y));
                Double deltaY2 = getModelData(Arrays.asList(ccd3_after_tangential_y), sysParameterList,PhysicalQuantityEnum.DELTAY);
                log.info("3逆向deltaY值:{}",Arrays.asList(ccd3_front_tangential_y));
                Double deltaY3 = getModelData(Arrays.asList(ccd3_front_tangential_y), sysParameterList,PhysicalQuantityEnum.DELTAY);*/



                Integer ccd2_after_dip_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_after_dip_y")).findFirst().get().getValue();
                Integer ccd2_front_dip_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_front_dip_y")).findFirst().get().getValue();
                // double dipX = (ccd2_after_dip_y + ccd2_front_dip_y) / 2 * 0.0047 / 2000;
                //Double dipX = getModelData(Arrays.asList(ccd2_after_dip_y,ccd2_front_dip_y), sysParameterList,PhysicalQuantityEnum.DIPY);
                //Double dipX2 = getModelData(Arrays.asList(ccd2_front_dip_y), sysParameterList,PhysicalQuantityEnum.DIPY);
                //Double dipX3 = getModelData(Arrays.asList(ccd2_after_dip_y), sysParameterList,PhysicalQuantityEnum.DIPY);


                Integer ccd1_after_dip_x = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_after_dip_x")).findFirst().get().getValue();
                Integer ccd1_front_dip_x = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_front_dip_x")).findFirst().get().getValue();
                //Double dipY = (ccd1_after_dip_x + ccd1_front_dip_x) / 2 * 0.0047 / 2000;
             /*   Double dipY = getModelData(Arrays.asList(ccd1_after_dip_x,ccd1_front_dip_x), sysParameterList,PhysicalQuantityEnum.DIPX);

                Double dipY2 = getModelData(Arrays.asList(ccd1_front_dip_x), sysParameterList,PhysicalQuantityEnum.DIPX);
                Double dipY3 = getModelData(Arrays.asList(ccd1_after_dip_x), sysParameterList,PhysicalQuantityEnum.DIPX);
*/
                if(one==null){
                    one = new AbsolutelyValueTable();
                    one.setMeasurChainCode(deptMeasuringInfoId);
                    one.setMeasurChainDirection(measuringTypeCode);
                    one.setDeptId(deviceId);
                    one.setzX(zx);
                    one.setnX(nx);
                    one.setzZ(zz);
                    one.setnZ(nz);
                    one.setCcd2FrontDipY(ccd2_front_dip_y);
                    one.setCcd2AfterDipY(ccd2_after_dip_y);

                    one.setCcd1AfterDipX(ccd1_after_dip_x);
                    one.setCcd1FrontDipX(ccd1_front_dip_x);

                    one.setCcd3AfterTangentialY(ccd3_after_tangential_y);
                    one.setCcd3FrontTangentialY(ccd3_front_tangential_y);
                    one.setInsertTime(System.currentTimeMillis());
                    iAbsolutelyValueTableService.save(one);
                    return PhysicalQuantityVO.builder().nZ(0.0).zX(0.0).zZ(0.0).nX(0.0).deltaY(0.0).dipY(0.0).dipX(0.0).build();
                }

				zx =  getDobleMath(zx - one.getzX(),zx2 - one.getzX(),zx3 - one.getzX());
               zz = getDobleMath(zz - one.getzZ(), zz2 - one.getzZ(), zz3 - one.getzZ());
               nx = getDobleMath(nx - one.getnX(), nx2 - one.getnX(), nx3 - one.getnX());
               nz = getDobleMath(nz - one.getnZ(), nz2 - one.getnZ(), nz3 - one.getnZ());

                List<Double> dobleMath1 = getDobleMath2(ccd3_front_tangential_y , ccd3_after_tangential_y ,one.getCcd3FrontTangentialY(),one.getCcd3AfterTangentialY());
                Double deltaY = getModelData2(dobleMath1.get(0), sysParameterList, PhysicalQuantityEnum.DELTAY)
                        - getModelData2(dobleMath1.get(1), sysParameterList, PhysicalQuantityEnum.DELTAY);


                List<Double> dobleMath2 = getDobleMath2(ccd2_after_dip_y , ccd2_front_dip_y,one.getCcd2AfterDipY(),one.getCcd2FrontDipY());
                Double dipX = getModelData2(dobleMath2.get(0), sysParameterList, PhysicalQuantityEnum.DIPY)
                        - getModelData2(dobleMath2.get(1), sysParameterList, PhysicalQuantityEnum.DIPY);



                List<Double> dobleMath3 = getDobleMath2(ccd1_front_dip_x , ccd1_after_dip_x,one.getCcd1FrontDipX() ,one.getCcd1AfterDipX());
                Double dipY = getModelData2(dobleMath3.get(0), sysParameterList,PhysicalQuantityEnum.DIPX)
                        - getModelData2(dobleMath3.get(1), sysParameterList, PhysicalQuantityEnum.DIPX);


                return PhysicalQuantityVO.builder().nZ(nz).zX(zx).zZ(zz).nX(nx).deltaY(deltaY).dipY(dipY).dipX(dipX).build();
            }
        }

    }
    /**
     * 基点测台计算工程量
     * @param measuringPlatformConfigVO
     * @param deviceList
     * @return
     */
    private AbsoluteValueVO getAbsoluteValue( List<String> lists,
                                              Map<String,DeviceTableVO> mapd ,
                                              MeasuringPlatformConfigVO measuringPlatformConfigVO,
                                              List<DeviceRuningStateDTO.DeviceListDTO> deviceList,
                                              Long id,
                                              Long acquisitionTime){
        double x = 0.0;
        double y = 0.0;
        Double z = null;
        Double alpha = null;
        Double beta = null;
        Double gama = null;

        List<DeviceRuningStateDTO.DeviceListDTO> collect = deviceList.stream().filter(l -> lists.contains(l.getDeviceId())).collect(Collectors.toList());
        List<SysParameter> sysParameterList = iSysParameterService.list(Wrappers.<SysParameter>query().lambda().eq(SysParameter::getMeasuringPlatformId,id));
        for (DeviceRuningStateDTO.DeviceListDTO deviceListDTO : collect) {
            // DeviceTable deviceTableVO = iDeviceTableService.getOne(Wrappers.<DeviceTable>query().lambda().eq(DeviceTable::getDeviceChannelId, deviceListDTO.getDeviceId()).eq(DeviceTable::getIsDeleted, 0));
            DeviceTableVO deviceTableVO = mapd.get(deviceListDTO.getDeviceId());

            List<DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO> attributeListDTO = deviceListDTO.getAttributeListDTO();
            switch (Objects.requireNonNull(ProductTypeEnum.getEnum(deviceTableVO.getProductId()))){
                case HT_GONIOMETER://华腾绕角仪
                    Integer ccd3_z_front = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd3_z_front")).findFirst().get().getValue();
                    Integer ccd3_z_after = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd3_z_after")).findFirst().get().getValue();
                    gama = getModelData(Arrays.asList(ccd3_z_front, ccd3_z_after), sysParameterList,PhysicalQuantityEnum.GAMA);
                    //gama = baseStationAlgorithm.getPhysicalQuantityDip(Double.valueOf(rich_z_K), ccd3_z_after, ccd3_z_front, Double.valueOf(rich_z_B));
                    //绕Z轴的旋转角
                    break;
                case HT_DOUBLE_MARKER://华腾双标仪
                    Integer ccd3_z_front1 = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd3_z_front")).findFirst().get().getValue();
                    Integer ccd3_z_after1 = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd3_z_after")).findFirst().get().getValue();
                    // z  = baseStationAlgorithm.getDeformation(ccd3_z_after1,ccd3_z_front1);
                    z = getModelData(Arrays.asList(ccd3_z_front1, ccd3_z_after1), sysParameterList,PhysicalQuantityEnum.Z);
                    break;
                case HT_PERPENDICULOMETER://华腾垂线仪
                    Integer ccd3_x_front = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd3_x_front")).findFirst().get().getValue();
                    Integer ccd3_x_after = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd3_x_after")).findFirst().get().getValue();
                    alpha = getModelData(Arrays.asList(ccd3_x_front, ccd3_x_after), sysParameterList,PhysicalQuantityEnum.ALPHA);
                    //alpha  = baseStationAlgorithm.getPhysicalQuantityDip(Double.valueOf(rich_x_K), ccd3_x_after, ccd3_x_front, Double.valueOf(rich_x_B));

                    Integer ccd1_y_front = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd1_y_front")).findFirst().get().getValue();
                    Integer ccd1_y_after = attributeListDTO.stream().filter(l -> l.getKey().equalsIgnoreCase("ccd1_y_after")).findFirst().get().getValue();
                    beta = getModelData(Arrays.asList(ccd1_y_front, ccd1_y_after), sysParameterList,PhysicalQuantityEnum.BETA);
                    //beta  = baseStationAlgorithm.getPhysicalQuantityDip(Double.valueOf(rich_y_K), ccd1_y_after, ccd1_y_front, Double.valueOf(rich_y_B));
                    break;
            }
        }
        List<String> strings = new ArrayList<>();
        if(measuringPlatformConfigVO!=null){//走配置的策略

            Integer xType = measuringPlatformConfigVO.getxType();
            if(xType!=null){
                if(xType.equals(DataStrategyEnum.MANUAL.getCode())){
                    x = Double.parseDouble(measuringPlatformConfigVO.getxValue());
                }else {
                    //未给出结算规则
                }

            }
            Integer yType = measuringPlatformConfigVO.getyType();
            if(yType!=null){
                if(yType.equals(DataStrategyEnum.MANUAL.getCode())){
                    y = Double.parseDouble(measuringPlatformConfigVO.getyValue());
                }else {
                    //未给出结算规则
                }
            }
            Integer zType = measuringPlatformConfigVO.getzType();
            if(zType!=null){
                if(zType.equals(DataStrategyEnum.MANUAL.getCode())){
                    z = Double.parseDouble(measuringPlatformConfigVO.getzValue());
                }else {
                    //未给出结算规则
                }
            }
            Map<String, Double> dataStrategy;
            Integer alphaType = measuringPlatformConfigVO.getAlphaType();
            if(alphaType!=null){
                if(alphaType.equals(DataStrategyEnum.MANUAL.getCode())){
                    alpha = Double.parseDouble(measuringPlatformConfigVO.getAlphaValue());
                }else if(alphaType.equals(DataStrategyEnum.COMPANY.getCode())){
                    dataStrategy =  getDataStrategy(id,measuringPlatformConfigVO.getAlphaValue(),measuringPlatformConfigVO.getPlatformTypeCode(),deviceList);//ccd1AfterDipx1
                    alpha = dataStrategy.get("dipX");
                    strings.add(measuringPlatformConfigVO.getAlphaValue());
                }
            }
            Integer betaType = measuringPlatformConfigVO.getBetaType();
            if(betaType!=null){
                if(betaType.equals(DataStrategyEnum.MANUAL.getCode())){
                    beta = Double.parseDouble(measuringPlatformConfigVO.getBetaValue());
                }else if(betaType.equals(DataStrategyEnum.COMPANY.getCode())){
                    dataStrategy = getDataStrategy(id,measuringPlatformConfigVO.getBetaValue(), measuringPlatformConfigVO.getPlatformTypeCode(), deviceList);
                    beta = dataStrategy.get("dipY");
                    strings.add(measuringPlatformConfigVO.getBetaValue());
                }
            }
            Integer gamaType = measuringPlatformConfigVO.getGamaType();
            if(gamaType!=null){
                if(gamaType.equals(DataStrategyEnum.MANUAL.getCode())){
                    gama = Double.parseDouble(measuringPlatformConfigVO.getGamaValue());
                }else if(gamaType.equals(DataStrategyEnum.COMPANY.getCode())){
                    //没有提供算法
                }
            }
        }

        return AbsoluteValueVO.builder().x(x).y(y).z(z).alpha(alpha).beta(beta).gama(gama).stringList(strings).build();
    }

    private Map<String,Double> getDataStrategy(Long measuringPlatformId,String value, Integer platformTypeCode, List<DeviceRuningStateDTO.DeviceListDTO> deviceList){
        Map<String,Double> map = new HashMap<>();
        List<DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO> attributeListDTO;
        DeviceTable deviceTableVO = iDeviceTableService.getOne(Wrappers.<DeviceTable>query().lambda().eq(DeviceTable::getDeviceId,value).eq(DeviceTable::getIsDeleted, 0));
        Optional<DeviceRuningStateDTO.DeviceListDTO> first = deviceList.stream().filter(x -> x.getDeviceId().equals( deviceTableVO.getDeviceChannelId())).findFirst();
        if(first.isPresent()){
            DeviceRuningStateDTO.DeviceListDTO deviceListDTO = first.get();
            attributeListDTO = deviceListDTO.getAttributeListDTO();
        }else {
            //查询默认值
            attributeListDTO = new ArrayList<>();
        }
        //高精度传感器一次量


        Integer ccd2_after_dip_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_after_dip_y")).findFirst().get().getValue();
        Integer ccd2_front_dip_y = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd2_front_dip_y")).findFirst().get().getValue();

        Integer ccd1_after_dip_x = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_after_dip_x")).findFirst().get().getValue();
        Integer ccd1_front_dip_x = attributeListDTO.stream().filter(x -> x.getKey().equalsIgnoreCase("ccd1_front_dip_x")).findFirst().get().getValue();


        AbsolutelyValueTable one = iAbsolutelyValueTableService.getOne(Wrappers.<AbsolutelyValueTable>lambdaQuery()
                .eq(AbsolutelyValueTable::getDeptId,value).eq(AbsolutelyValueTable::getMeasurChainDirection,platformTypeCode));

        List<SysParameter> sysParameterList = iSysParameterService.list(Wrappers.<SysParameter>query().lambda().eq(SysParameter::getMeasuringPlatformId,measuringPlatformId));
        if(one==null){
/*			Double v3 =  getModelData(Arrays.asList(ccd2_after_dip_y,ccd2_front_dip_y),sysParameterList,PhysicalQuantityEnum.DIPY);
			Double v2 =  getModelData(Arrays.asList(ccd1_after_dip_x,ccd1_front_dip_x),sysParameterList,PhysicalQuantityEnum.DIPX);*/
            one = new AbsolutelyValueTable();
            one.setMeasurChainDirection(platformTypeCode);
            one.setDeptId(value);
			one.setCcd2FrontDipY(ccd2_front_dip_y);
			one.setCcd2AfterDipY(ccd2_after_dip_y);

			one.setCcd1AfterDipX(ccd1_after_dip_x);
			one.setCcd1FrontDipX(ccd1_front_dip_x);
   /*         one.setDipX(v3);
            one.setDipY(v2);*/
            iAbsolutelyValueTableService.save(one);

            map.put("dipX",0.0);
            map.put("dipY",0.0);
            return map;
        }else {
			List<Double> dobleMath2 = getDobleMath2(ccd2_after_dip_y , ccd2_front_dip_y,one.getCcd2AfterDipY(),one.getCcd2FrontDipY());
			Double v1 = getModelData2(dobleMath2.get(0), sysParameterList, PhysicalQuantityEnum.DIPY)
				- getModelData2(dobleMath2.get(1), sysParameterList, PhysicalQuantityEnum.DIPY);



			List<Double> dobleMath3 = getDobleMath2(ccd1_front_dip_x , ccd1_after_dip_x,one.getCcd1FrontDipX() ,one.getCcd1AfterDipX());
			Double v = getModelData2(dobleMath3.get(0), sysParameterList,PhysicalQuantityEnum.DIPX)
				- getModelData2(dobleMath3.get(1), sysParameterList, PhysicalQuantityEnum.DIPX);

/*            Double v =  getModelData(Arrays.asList(ccd1_after_dip_x ,ccd1_front_dip_x ),sysParameterList,PhysicalQuantityEnum.DIPX);
            Double v1 =  getModelData(Arrays.asList(ccd2_after_dip_y ,ccd2_front_dip_y ),sysParameterList,PhysicalQuantityEnum.DIPY);*/
            map.put("dipX",v1);
            map.put("dipY",v);
            log.info("高精度传感器计算map:{}",map);
            return map;
        }




    }
    private void saveMeasuringChainDetails(MeasuringChainDetailsVO measuringChainDetailsVO,String acquisitionFlag) {
        List<MeasuringPlatformVO> measuringPlatformVOS = measuringChainDetailsVO.getMeasuringPlatformVOS();
        if (!Func.isEmpty(measuringPlatformVOS)) {

            List<MeasuringPlatformVO> collect = measuringPlatformVOS.stream().sorted(Comparator.comparing(MeasuringPlatformVO::getSort).reversed()).collect(Collectors.toList());
            log.info("入库开始:{}",collect);
            List<PhysicalValueTable> physicalValueTables = collect.stream().map(measuringPlatformVO -> {
                PhysicalValueTable physicalValueTable = new PhysicalValueTable();
                physicalValueTable.setGatherEvent(measuringChainDetailsVO.getGatherEvent());
                physicalValueTable.setGatherTime(measuringChainDetailsVO.getGatherTime());
                physicalValueTable.setMeasurChainCode(measuringChainDetailsVO.getMeasurChainCode());
                physicalValueTable.setMeasurChain(measuringChainDetailsVO.getMeasurChain());
                physicalValueTable.setMeasurChainDirection(measuringChainDetailsVO.getMeasurChainDirection());
                physicalValueTable.setMeasurChainDirectionName(measuringChainDetailsVO.getMeasurChainDirection() == 1 ? "正向" : "逆向");
                physicalValueTable.setInsertTime(System.currentTimeMillis());
                physicalValueTable.setBenchTypeCode(measuringPlatformVO.getBenchTypeCode());
                physicalValueTable.setBenchTypeName(measuringPlatformVO.getBenchTypeName());
                physicalValueTable.setDeptId(measuringPlatformVO.getMeasuringPlatformId());
                physicalValueTable.setDeptName(measuringPlatformVO.getMeasuringPlatformName());
                PhysicalQuantityVO physicalQuantityVO = measuringPlatformVO.getPhysicalQuantityVO();
                BigDecimal nx = new BigDecimal(physicalQuantityVO.getNX()).setScale(10, RoundingMode.DOWN);
                physicalValueTable.setnX(nx.doubleValue());
                BigDecimal nz = new BigDecimal(physicalQuantityVO.getNZ()).setScale(10, RoundingMode.DOWN);
                physicalValueTable.setnZ(nz.doubleValue());
                BigDecimal zz = new BigDecimal(physicalQuantityVO.getZZ()).setScale(10, RoundingMode.DOWN);
                physicalValueTable.setzZ(zz.doubleValue());
                BigDecimal zx = new BigDecimal(physicalQuantityVO.getZX()).setScale(10, RoundingMode.DOWN);
                physicalValueTable.setzX(zx.doubleValue());
                BigDecimal deltaY = new BigDecimal(physicalQuantityVO.getDeltaY()).setScale(10, RoundingMode.DOWN);
                physicalValueTable.setDeltaY(deltaY.doubleValue());
                BigDecimal dipy = new BigDecimal(physicalQuantityVO.getDipY()).setScale(10, RoundingMode.DOWN);
                physicalValueTable.setDipY(dipy.doubleValue());
                BigDecimal dipx = new BigDecimal(physicalQuantityVO.getDipX()).setScale(10, RoundingMode.DOWN);
                physicalValueTable.setDipX(dipx.doubleValue());
                physicalValueTable.setAcquisitionFlag(acquisitionFlag);
                return physicalValueTable;
            }).collect(Collectors.toList());

            iPhysicalValueTableService.saveBatch(physicalValueTables);


            List<EngineerValueTable> engineerValueTables = collect.stream().map(measuringPlatformVO -> {
                AbsoluteValueVO absoluteValueVO = measuringPlatformVO.getAbsoluteValueVO();
                EngineerValueTable engineerValueTable = new EngineerValueTable();
                engineerValueTable.setGatherEvent(measuringChainDetailsVO.getGatherEvent());
                engineerValueTable.setGatherTime(measuringChainDetailsVO.getGatherTime());
                engineerValueTable.setMeasurChainCode(measuringChainDetailsVO.getMeasurChainCode());
                engineerValueTable.setMeasurChain(measuringChainDetailsVO.getMeasurChain());
                engineerValueTable.setMeasurChainDirection(measuringChainDetailsVO.getMeasurChainDirection());
                engineerValueTable.setMeasurChainDirectionName(measuringChainDetailsVO.getMeasurChainDirection() == 1 ? "正向" : "逆向");
                engineerValueTable.setInsertTime(System.currentTimeMillis());
                engineerValueTable.setBenchTypeCode(measuringPlatformVO.getBenchTypeCode());
                engineerValueTable.setBenchTypeName(measuringPlatformVO.getBenchTypeName());
                engineerValueTable.setDeptId(measuringPlatformVO.getMeasuringPlatformId());
                engineerValueTable.setDeptName(measuringPlatformVO.getMeasuringPlatformName());

                BigDecimal x = new BigDecimal(absoluteValueVO.getX()).setScale(10, RoundingMode.DOWN);
                engineerValueTable.setX(x.doubleValue());

                BigDecimal y = new BigDecimal(absoluteValueVO.getY()).setScale(10, RoundingMode.DOWN);
                engineerValueTable.setY(y.doubleValue());

                BigDecimal z = new BigDecimal(absoluteValueVO.getZ()).setScale(10, RoundingMode.DOWN);
                engineerValueTable.setZ(z.doubleValue());

                BigDecimal alpha = new BigDecimal(absoluteValueVO.getAlpha()).setScale(10, RoundingMode.DOWN);
                engineerValueTable.setAlpha(alpha.doubleValue());

                BigDecimal bate = new BigDecimal(absoluteValueVO.getBeta()).setScale(10, RoundingMode.DOWN);
                engineerValueTable.setBeta(bate.doubleValue());

                BigDecimal gama = new BigDecimal(absoluteValueVO.getGama()).setScale(10, RoundingMode.DOWN);
                engineerValueTable.setGama(gama.doubleValue());


                engineerValueTable.setAcquisitionFlag(acquisitionFlag);
                return engineerValueTable;

            }).collect(Collectors.toList());
            log.info("计算工程量为:{}",engineerValueTables);

            iEngineerValueTableService.saveBatch(engineerValueTables);

            collect.forEach(measuringPlatformVO->{
     /*           List<OnceValueVO> onceValueVOList = measuringPlatformVO.getOnceValueVOList();
                if(!Func.isEmpty(onceValueVOList)){
                    for (OnceValueVO l1 : onceValueVOList) {

                        List<MeasuringPointTable> list = iMeasuringPointTableService.list(Wrappers.<MeasuringPointTable>lambdaQuery()
                                .eq(MeasuringPointTable::getGatherTime, measuringChainDetailsVO.getGatherTime())
                                .eq(MeasuringPointTable::getMeasurChainCode, measuringChainDetailsVO.getMeasurChainCode())
                                .eq(MeasuringPointTable::getMeasurChainDirection, measuringChainDetailsVO.getMeasurChainDirection())
                                .eq(MeasuringPointTable::getDeviceId, l1.getDeviceId())
                                .eq(MeasuringPointTable::getPropertieId, l1.getPropertieId()));
                        if(CollectionUtil.isEmpty(list)){
                            MeasuringPointTable measuringPointTable = new MeasuringPointTable();
                            measuringPointTable.setGatherTime(measuringChainDetailsVO.getGatherTime());
                            measuringPointTable.setGatherEvent(measuringChainDetailsVO.getGatherEvent());
                            measuringPointTable.setMeasurChainCode(measuringChainDetailsVO.getMeasurChainCode());
                            measuringPointTable.setMeasurChain(measuringChainDetailsVO.getMeasurChain());
                            measuringPointTable.setMeasurChainDirection(measuringChainDetailsVO.getMeasurChainDirection());
                            measuringPointTable.setMeasurChainDirectionName(measuringChainDetailsVO.getMeasurChainDirection() == 1 ? "正向" : "逆向");
                            measuringPointTable.setInsertTime(System.currentTimeMillis());
                            measuringPointTable.setBenchTypeCode(measuringPlatformVO.getBenchTypeCode());
                            measuringPointTable.setBenchTypeName(measuringPlatformVO.getBenchTypeName());
                            measuringPointTable.setDeptId(measuringPlatformVO.getMeasuringPlatformId());
                            measuringPointTable.setDeptName(measuringPlatformVO.getMeasuringPlatformName());
                            measuringPointTable.setDeviceId(l1.getDeviceId());
                            measuringPointTable.setDeviceName(l1.getDeviceName());
                            measuringPointTable.setPropertieId(l1.getPropertieId());
                            measuringPointTable.setPropertieName(l1.getPropertieName());
                            measuringPointTable.setOnceValue(Integer.parseInt(l1.getOnceValue()));
                            measuringPointTable.setInsertTime(System.currentTimeMillis());
                            measuringPointTable.setRietschlePressure(measuringChainDetailsVO.getRietschlePressure());
                            measuringPointTable.setAcquisitionFlag(acquisitionFlag);
                            iMeasuringPointTableService.save(measuringPointTable);
                        }


                    }
                }*/
            });

        }
    }
    private List<OnceValueVO> getOnceValue(DeviceRuningStateDTO.DeviceListDTO deviceListDTO,Long gatherTime,String deviceId,String acquisitionFlag){
        List<OnceValueVO> onceValueVOList = new ArrayList<>();
        DeviceTable deviceTableVO = iDeviceTableService.getOne(Wrappers.<DeviceTable>query().lambda().eq(DeviceTable::getDeviceId,deviceId).eq(DeviceTable::getIsDeleted, 0));
        EquipmentRuningState equipmentRuningState4 = new EquipmentRuningState();
        equipmentRuningState4.setEquipmentId(deviceTableVO.getDeviceId());
        equipmentRuningState4.setInsertTime(gatherTime);
        equipmentRuningState4.setUpdateTime(gatherTime);
        ProductModelProperties productModelProperties = productModelPropertiesService.getOne(new QueryWrapper<ProductModelProperties>().lambda()
                .eq(ProductModelProperties::getProductId,deviceTableVO.getProductId()));
        String propertiesJson = productModelProperties.getPropertiesJson();
        JSONArray jsonArray = JSONArray.parseArray(propertiesJson);
        JSONObject jsonObject = new JSONObject();
        List<DeviceRuningStateDTO.DeviceListDTO.AttributeListDTO> attributeListDTO = deviceListDTO.getAttributeListDTO();
        if(Func.isEmpty(attributeListDTO)){
            return onceValueVOList;
        }
        attributeListDTO.forEach(l->{
            EquipmentRuningState equipmentRuningState1 = new EquipmentRuningState();
            equipmentRuningState1.setEquipmentId(deviceTableVO.getDeviceId());
            equipmentRuningState1.setInsertTime(gatherTime);
            equipmentRuningState1.setUpdateTime(gatherTime);
            equipmentRuningState1.setPropertieId(l.getKey());
            Map<String, Object> map = new HashMap<>();
            map.put(l.getKey(), l.getValue());
            equipmentRuningState1.setJsonText(JSONObject.toJSONString(map));
            iEquipmentRuningStateService.remove(Wrappers.<EquipmentRuningState>query().lambda().eq(EquipmentRuningState::getEquipmentId, deviceTableVO.getDeviceId()).eq(EquipmentRuningState::getPropertieId, l.getKey()));
            iEquipmentRuningStateService.save(equipmentRuningState1);
            jsonObject.put(l.getKey(), l.getValue());

            String propertieName = null;
            Optional<Object> id = jsonArray.stream().filter(j -> ((JSONObject) j).get("id").toString().equalsIgnoreCase(l.getKey())).findFirst();
            if(id.isPresent()){
                JSONObject o = (JSONObject) id.get();
                propertieName = o.get("name").toString();
            }
            onceValueVOList.add( OnceValueVO.builder().onceValue(String.valueOf(l.getValue()))
                    .deviceName(deviceTableVO.getDeviceName())
                    .deviceId(deviceTableVO.getDeviceId())
                    .deviceChannelId(deviceTableVO.getDeviceChannelId()).propertieId(l.getKey()).propertieName(propertieName).build());
        });
        equipmentRuningState4.setJsonText(jsonObject.toJSONString());
        iEquipmentRuningStateService.insertEquipmentRuningState(equipmentRuningState4);
    /*    EquipmentLogManagement equipmentLogManagement = new EquipmentLogManagement();
        equipmentLogManagement.setCmd("cmd");
        equipmentLogManagement.setCmdSeq(String.valueOf(System.currentTimeMillis()));
        equipmentLogManagement.setEquipmentId(deviceTableVO.getDeviceId());
        equipmentLogManagement.setCreateTime(System.currentTimeMillis());
        equipmentLogManagement.setIsDeleted(0);
        equipmentLogManagement.setLogType(1);
        equipmentLogManagement.setPullMessageContent(attributeListDTO.toString());
        equipmentLogManagement.setPullMessageTime(System.currentTimeMillis());
        equipmentLogManagementService.save(equipmentLogManagement);*/
        return onceValueVOList;

    }

    private Double getModelData(List<Integer> x, List<SysParameter> sysParameterList,PhysicalQuantityEnum physicalQuantityEnum){
        log.info("标定系数physicalQuantityEnum:{}",physicalQuantityEnum);
        log.info("CCD值:{}",x);
        log.info("标定系数sysParameterList:{}",sysParameterList);

        Double collect = x.stream().collect(Collectors.averagingDouble(item -> item));
        SysParameter first = sysParameterList.stream().filter(l ->
                l.getParameterKey().equals(physicalQuantityEnum.getCode())
                        && l.getMinValue() <= collect
                        && l.getMaxValue() > collect).findFirst().get();
        double v = first.getDipXK() * collect + first.getDipXB();
        BigDecimal bigDecimal = new BigDecimal(v).setScale(14, RoundingMode.DOWN);
        return bigDecimal.doubleValue();
    }

    private Double getModelData2(Double x, List<SysParameter> sysParameterList,PhysicalQuantityEnum physicalQuantityEnum){
        log.info("标定系数physicalQuantityEnum:{}",physicalQuantityEnum);
        log.info("CCD值:{}",x);
        log.info("标定系数sysParameterList:{}",sysParameterList);
        SysParameter first = sysParameterList.stream().filter(l ->
                l.getParameterKey().equals(physicalQuantityEnum.getCode())
                        && l.getMinValue() <= x
                        && l.getMaxValue() > x).findFirst().get();
        double v = first.getDipXK() * x + first.getDipXB();
        BigDecimal bigDecimal = new BigDecimal(v).setScale(14, RoundingMode.DOWN);
        return bigDecimal.doubleValue();
    }


    private void pullData(BigDecimal d,String str,String connectionId,Integer code){
        try {
            if(StringUtils.isEmpty(connectionId)){
                return;
            }
            ChannelGroup channels = WebSokcetChannelGroup.channels;
            Map<String, List<String>> map = WebSokcetChannelGroup.MAP;
            if(map.containsKey("COLLECT_DATA")){
                List<String> stringList = map.get("COLLECT_DATA");
                if(Func.isEmpty(stringList)){
                    return;
                }
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("proportion",d);
                jsonObject.put("msg",str);
                jsonObject.put("code",code);
                Optional<Channel> first = channels.stream().filter(x ->x.attr(CONNECTION_ID).get().contains(connectionId)).findFirst();
                if(first.isPresent()){
                    ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
                    channelGroup.add(first.get());
                    log.info("推送数据:{}",JSONObject.toJSONString(jsonObject));
                    channelGroup.writeAndFlush(new TextWebSocketFrame(JSONObject.toJSONString(jsonObject)));
                }
            }
        }catch (Exception e){
            log.info("推送数据异常");

        }
    }
    public List<Double> getDobleMath2(Integer x1,Integer x2,Integer x3,Integer x4){
        Double abs = (double) Math.abs(x1 - x3);
        Double abs1 = (double) Math.abs(x2 - x4);
        Double collect1 = Stream.of(x1, x2).collect(Collectors.averagingDouble(item -> item));
        Double collect2 = Stream.of(x3, x4).collect(Collectors.averagingDouble(item -> item));
        Double collect = Math.abs(collect1 - collect2);
        Double min = Collections.min( Arrays.asList(abs, abs1,collect));
        if(abs.equals(min)){
            return Arrays.asList((double) x1,(double) x3);
        }else  if(abs1.equals(min)){
            return Arrays.asList((double) x2,(double) x4);
        }else {
            return Arrays.asList(collect1,collect2);
        }
    }
    public Double getDobleMath(Double x1,Double x2,Double x3){

        Double min = Collections.min(Arrays.asList(Math.abs(x1), Math.abs(x2), Math.abs(x3)));
        if(Math.abs(x1)==min){
            return x1;
        }else if(Math.abs(x2)==min){
            return x2;
        }else {
            return x3;
        }
    }


}
